import scrapy


class DangSpider(scrapy.Spider):
    name = "dang"
    # allowed_domains = ["category.dangdang.com"]
    # 1.修改start_urls
    start_urls = ["https://category.dangdang.com/cp01.34.04.00.00.00.html"]

    def parse(self, response):
        book_nodes=response.xpath('//ul[@class="bigimg"]/li')#.extract()

        for book in book_nodes:
            info={}
            info["title"]=book.xpath('./p[1]/a/@title').extract_first()
            info["price"]=book.xpath('./p[3]/span[1]/text()').extract_first()
            info["rating"] = book.xpath('./p[4]/span[1]/span[1]/@style').extract_first()
            info["comments_count"] = book.xpath('./p[4]/a/text()').extract_first()
            yield info

        next_page=response.xpath('//li[@class="next"]/a/@href').extract_first()
        if next_page is not None:
            yield response.follow(next_page,self.parse)

